package com.shop.web.admin;

import com.shop.annotation.MyLog;
import com.shop.entity.SalesUser;
import com.shop.entity.pojo.ResultBean;
import com.shop.service.SalesUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/salesuser")
public class AdminSalesUserController {
    @Autowired
    private SalesUserService salesUserService;

    /**
     * 打开用户列表页面
     * @return
     */
    @RequestMapping("/toList.html")
    public String toList() {
        return "admin/salesuser/list";
    }

    /**
     * 打开编辑页面
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/toEdit.html")
    public String toEdit(int id, Map<String, Object> map) {
        SalesUser salesUser = salesUserService.findById(id);
        map.put("salesuser", salesUser);
        return "admin/salesuser/edit";
    }

    /**
     * 打开添加分类页面
     *
     * @return
     */
    @RequestMapping("/toAdd.html")
    public String toAdd() {return "admin/salesuser/add";}

    /**
     * 获取所有用户列表
     *
     * @param pageindex
     * @return
     */
    @ResponseBody
    @RequestMapping("/list.do")
    public ResultBean<List<SalesUser>> findAllUser(int pageindex,
                                              @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) {
        Pageable pageable = new PageRequest(pageindex, pageSize, null);
        List<SalesUser> salesUsers = salesUserService.findAll(pageable).getContent();
        return new ResultBean<>(salesUsers);
    }

    @ResponseBody
    @RequestMapping("/getTotal.do")
    public ResultBean<Integer> geTotal() {
        Pageable pageable = new PageRequest(1, 15, null);
        int total = (int) salesUserService.findAll(pageable).getTotalElements();
        return new ResultBean<>(total);
    }

    @ResponseBody
    @RequestMapping("/del.do")
    @MyLog(title = "管理员模块", content = "删除销售人员信息")
    public ResultBean<Boolean> del(int id) {
        salesUserService.delById(id);
        return new ResultBean<>(true);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/add.do")
    @MyLog(title = "管理员模块", content = "添加销售人员信息")
    public ResultBean<Boolean> add(String username, String password) {
        SalesUser salesUser = new SalesUser();
        salesUser.setUsername(username);
        salesUser.setPassword(password);
        salesUserService.create(salesUser);
        return new ResultBean<>(true);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/update.do")
    @MyLog(title = "管理员模块", content = "修改销售人员信息")
    public ResultBean<Boolean> update(int id,String username,
                                      String password) {
        // 更新前先查询
        SalesUser salesUser = salesUserService.findById(id);
        salesUser.setId(id);
        salesUser.setUsername(username);
        salesUser.setPassword(password);
        salesUserService.update(salesUser);
        return new ResultBean<>(true);
    }

    /**
     * 验证用户名是否唯一
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkUsername.do")
    public ResultBean<Boolean> checkUsername(String username){
        List<SalesUser> salesUsers = salesUserService.findByUsername(username);
        if (salesUsers==null||salesUsers.isEmpty()){
            return new ResultBean<>(true);
        }
        return new ResultBean<>(false);
    }
}
